package com.team.easybuy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.easybuy.entity.Car;
import com.team.easybuy.entity.Order;
import com.team.easybuy.entity.OrderDetail;
import com.team.easybuy.entity.Product;
import com.team.easybuy.mapper.OrderDetailMapper;
import com.team.easybuy.mapper.OrderMapper;
import com.team.easybuy.service.OrderService;
import com.team.easybuy.service.ProductService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private ProductService productService;
    @Resource
    private RedisTemplate redisTemplate;


    @Override
    public List<Order> getOrderList(Integer userId, Integer isPay,Integer refund) {
        return orderMapper.selectOrderList(userId, isPay,refund);
    }

    @Override
    public PageInfo<Order> getOrderPage(Integer userId, Integer isPay,Integer refund, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderMapper.selectOrderList(userId, isPay,refund);
        return new PageInfo<Order>(orderList);
    }

    @Override
    public List<OrderDetail> getOrderDetailList(Integer orderId) {
        return orderDetailMapper.selectOrderDetailList(orderId);
    }

    @Override
    public boolean delOrder(Integer id) {
        int count = orderMapper.updateIsDelete(id);
        if (count == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean calorder(Integer id) {
        List<OrderDetail> orderDetailList = orderDetailMapper.selectOrderDetailList(id);
        productService.updateAddStock(orderDetailList);
        int count = orderMapper.updateIsPay(id, 2);
        if (count == 1) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public Order addOrder(Order order, List<OrderDetail> list, List<Car> carList) {
        if (orderMapper.addOrder(order) > 0) {
            for (OrderDetail detail : list) {
                detail.setOrderId(order.getId());
            }
            if (orderDetailMapper.addOrderDetailList(list)) {
                productService.updateStock(carList);
                if (redisTemplate.hasKey("car")) {
                    Map<Integer, List<Car>> myCar = (Map<Integer, List<Car>>) redisTemplate.opsForValue().get("car");
                    List<Car> cars = myCar.get(order.getUserId());
                    cars.clear();
                    myCar.put(order.getUserId(), cars);
                    redisTemplate.opsForValue().set("car", myCar);
                    return order;
                }
            }
        }
        return null;
    }

    @Override
    public Order loadOrder(Integer id) {
        return orderMapper.loadOrder(id);
    }

    @Override
    public void updatePayType(String out_trade_no) {
        orderMapper.updatePayType(out_trade_no);
    }

    @Override
    public void refund(Integer id) {
        orderMapper.refund(id);
    }

    @Override
    public boolean updateRefund(Integer id) {
        return orderMapper.updateRefund(id);
    }

    @Scheduled(cron = "0 */1 * * * ?")
    @Async
    @Transactional
    public void updatePayTypeIfNoPay() {
        System.out.println("开始调用");
        List<Order> list = orderMapper.selectAll();
        long time = new Date().getTime();
        for (Order order : list) {
            long orderTime = order.getCreateTime().getTime();
            if ((time - orderTime) / 1000 / 60 > 15) {
                orderMapper.updatePayTypeIfNoPay(order.getId());
                List<OrderDetail> orderDetailList = orderDetailMapper.selectByOrderId(order.getId());
                productService.updateAddStock(orderDetailList);
            }
        }
    }
}
